﻿

function GetPageUrlByPageTypeId(PageType) {
    var pageurl = "";
    switch (PageType) {
        case 0:
            pageurl = "javascript:;";
            break;
        case 1:
            pageurl = "List.aspx?t=Article";
            break;
        case 2:
            pageurl = "List.aspx?t=Product";
            break;
        case 3:
            pageurl = "SinglePage.aspx?t=SinglePage&action=editSinglePage";
            break;
        case 4:
            pageurl = "List.aspx?t=Banner";
            break;
        case 5:
            pageurl = "List.aspx?t=Cases";
            break;
        case 6:
            pageurl = "List.aspx?t=Solution";
            break;
        case 7:
            pageurl = "List.aspx?t=Video";
            break;
        case 8:
            pageurl = "List.aspx?t=DownLoad";
            break;
        case 9:
            pageurl = "List.aspx?t=Faq";
            break;
        case 10:
            pageurl = "List.aspx?t=FeedBack";
            break;
        case 11:
            pageurl = "List.aspx?t=Job";
            break;
        case 12:
            pageurl = "List.aspx?t=Links";
            break;
        case 13:
            pageurl = "List.aspx?t=Member";
            break;
        case 14:
            pageurl = "List.aspx?t=Picture";
            break;
        default:
            pageurl = "javascript:;";
            break;
    }
    return pageurl;
}

function GetTemplateTypeByPageType(PageType)
{
    var TemplateName = "";
    switch (PageType)
    {
        case 0:
            TemplateName = "";
            break;
        case 1:
            TemplateName = "News";
            break;
        case 2:
            TemplateName = "Product";
            break;
        case 3:
            TemplateName = "SinglePage";
            break;
        case 4:
            TemplateName = "Banner";
            break;
        case 5:
            TemplateName = "Case";
            break;
        case 6:
            TemplateName = "Solution";
            break;
        case 7:
            TemplateName = "Video";
            break;
        case 8:
            TemplateName = "DownLoad";
            break;
        case 9:
            TemplateName = "FAQ";
            break;
        case 10:
            TemplateName = "FeedBack";
            break;
        case 11:
            TemplateName = "Job";
            break;
        case 12:
            TemplateName = "Link";
            break;
        case 13:
            TemplateName = "Member";
            break;
        case 14:
            TemplateName = "Picture";
            break;
        default:
            break;
    }
    return TemplateName;
}


//重新初始化模板选择
//function FormatSkinSelect(templates) {
//    function formatState(state) {
//        //console.log($(state.element).attr("data-imgurl"))
//        if (!state.id) { return state.text; }
//        var s = $(state.element);
//        var $state = $(
//          '<div>' +
//            '<div style="float:left;"><img src="' + s.attr("data-imgurl") + '" style="width:100px;height:100px"  /></div>' +
//            '<div style="float:left;"><div style="margin-left:5px; margin-bottom:5px;">' + s.attr("data-title") + '</div><div style="margin-left:5px;">' + s.attr("data-desc") + '</div></div>' +
//            '<div style="clear:both;"></div>' +
//          '</div>'
//        );
//        return $state;
//    }

//    $("#single-prepend-text-SkinName").find("option").remove();
//    for (var i = 0; i < templates.length; i++) {
//        $("#single-prepend-text-SkinName").append('<option value="' + templates[i].Path + '" data-title="' + templates[i].Name + '" data-path="' + templates[i].Path + '" data-desc="' + templates[i].Desc + '" data-index="' + i + '" data-imgurl="' + templates[i].ImgUrl + '">' + templates[i].Name + '</option>');
//    }

//    //带图片
//    $("#single-prepend-text-SkinName").select2({
//        templateResult: formatState,
//        templateSelection: formatState,
//        width: "100%"
//    });

//    $("#single-prepend-text-SkinName").next("span").find(".select2-selection").css({ "height": "114px", "padding-left": "7px" });


//}

////初始化模板选择
//function InitSkinSelect(PageTypeId)
//{
//    var curPageTypeTemplate = templates[GetTemplateTypeByPageType(PageTypeId)];
//    FormatSkinSelect(curPageTypeTemplate["Templates"]);
//}

//初始化页面类型选择
//function InitSetPageType()
//{
//    var PageTypeId = parseInt($("select[field='PageType']").val() == "" ? 0 : $("select[field='PageType']").val());
//    if (PageTypeId == 0 || PageTypeId == 4) {
//        //综合页和Banner是没有单独页面的
//        $("#form-WebPath").hide();
//        $("#form-SkinName").hide();
//    }
//    else {
//        $("#form-WebPath").show();
//        $("#form-SkinName").show();
//        InitSkinSelect(PageTypeId);
//    }
//}

$(function () {

    $(".NoUseCharInWebPath").on("click", function () {
        alert("<h5>只允许输入/和英文字母和数字 , 禁止使用空格 .</h5> <h5>如果是外链 , 请加上 http:// (如果是 https 请加 https://) .</h5>");
    });

    //var ChannelTemplateFields="";
    //var CurTemplateTable = "";
    $("select[field='PageType']").on("change", function () {
        var PageTypeId = parseInt($(this).val());
        model["ManagePageUrl"] = GetPageUrlByPageTypeId(parseInt(PageTypeId));
        $("input[field='ManagePageUrl']").val(model["ManagePageUrl"])
        if (PageTypeId == 20) {
            $("input[field='ManagePageUrl']").closest(".form-group").removeClass("hide");
        } else {
            $("input[field='ManagePageUrl']").closest(".form-group").addClass("hide");
        }
        //InitSetPageType();
    });

    //InitSetPageType();

    
    //$("select[field='SkinName']").val(model.SkinName).trigger("change");
   //function InitSkinAndPageType() {
   //     var PageTypeId = parseInt($("select[field='PageType']").val() == "" ? 0 : $("select[field='PageType']").val());
   //     if (PageTypeId != 0 && PageTypeId != 4 && PageTypeId != 12) {
   //         var CurPath = model.EnTitle.replace(/ /g, "_") + "/Default";
   //         var CurPageTemplates = templates[GetTemplateTypeByPageType(PageTypeId)].Templates;
   //         console.log(CurPageTemplates)
   //         CurTemplateTable = templates[GetTemplateTypeByPageType(PageTypeId)].Table;
   //         var CurFields = CurPageTemplates.Where("o.Path=='" + CurPath + "'");
   //         if (CurFields.length > 0) {
   //             ChannelTemplateFields = CurFields[0].Fields;
   //         }
   //         model["SkinName"] = CurPath;
   //         ChannelTemplateFields = JSON.stringify(ChannelTemplateFields);
   //     }
   //     else {
   //         model["SkinName"] = "";
   //     }
   // }
    //添加的时候不动
   //if (IsAddAction()) {
   //    var PageTypeId = parseInt($("select[field='PageType']").val() == "" ? 0 : $("select[field='PageType']").val());
   //    if (PageTypeId != 0 && PageTypeId != 4 && PageTypeId != 12) {
   //        var CurPath = $("[field='EnTitle']").val().replace(/ /g, "_") + "/Default";
   //        var CurPageTemplates = templates[GetTemplateTypeByPageType(PageTypeId)].Templates;
   //        console.log(CurPageTemplates)
   //        CurTemplateTable = templates[GetTemplateTypeByPageType(PageTypeId)].Table;
   //        var CurFields = CurPageTemplates.Where("o.Path=='" + CurPath + "'");
   //        if (CurFields.length > 0) {
   //            ChannelTemplateFields = CurFields[0].Fields;
   //        }
   //        model["SkinName"] = CurPath;
   //        ChannelTemplateFields = JSON.stringify(ChannelTemplateFields);
   //    }
   //    else {
   //        model["SkinName"] = "";
   //    }
   //} else {
   //    InitSkinAndPageType();
   //}
    

    

    //模板选择后的
    //$("select[field='SkinName']").on("change", function () {
    //    InitSkinAndPageType();
    //});

    var pid = GetQs("parentid");
    if (pid != "") {
        $("select[field='ParentId']").val(pid).trigger("change");
    }


    //提交按钮的功能 后台统一提交地址 统管编辑和添加
    $(".channel-submit").on("click", function () {
        //检查一遍输入状态
        if (!CheckVerify()) {
            notice("请检查您的输入项是否有错误", "error");
            return false;
        }
        //检查必要参数
        if (GetQs("action") == "" || GetQs("t") == "") {
            alert("您的访问异常 , 请重新登录后再尝试操作", function () {
                window.location.href = appConfig.adminPath + "login.aspx?action=loginout";
            });
            return false;
        }
        var _this = this;
        model = MakeModel();

        /*张浩 2017.06.27添加。修改栏目信息的时候ManagePageUrl字段解码之后再添加到数据库里面*/
        //model.ManagePageUrl = unescape(model.ManagePageUrl);

        var data = { model: JSON.stringify(model), table: GetQs("t") };
        $(_this).button('loading');
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", GetQs("action"), data, function (f, d) {
            $(_this).button('reset');
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    alert("操作成功", function () {
                        if (IsEditAction() || IsAddAction()) { $.Cookie_Del(AutoSave_CookieName()) }
                        setTimeout(function () {
                            if (GetQs("t") == "Versions") {
                                window.location.href = window.location.href;
                            } else {
                                JumpList();
                            }
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -10) {
                    alert("栏目目录已存在 , 请重新设置栏目目录<h5 class='font-red'><b>栏目目录不允许和栏目列表中的任何一条重复 (包括已锁定的栏目)<b></h5>");
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });

});

